﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Net.Mail;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;

namespace outREACH
{
    public partial class NGOs : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox CodeNumberTextBox;
        protected System.Web.UI.WebControls.Button SubmitButton;
        protected System.Web.UI.WebControls.Label MessageLabel;

        // For generating random numbers.
        private Random random = new Random();
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Button5.OnClientClick = "return popup('Terms_and_Conditions.aspx', 'Terms_and_Conditions')";
            if (!this.IsPostBack)
            {
                // Create a random code and store it in the Session object.
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }

            
        }

        private string GenerateRandomCode()
        {
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, this.random.Next(10).ToString());
            return s;
        }

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            NGO n = new NGO();
            n.ngoName = TextBox21.Text.ToString();
            n.ngoUsername = TextBox22.Text.ToString();
            n.ngoPassword = TextBox23.Text.ToString();
            n.ngoAddress = TextBox25.Text.ToString();
            n.Zipcode = TextBox26.Text.ToString();
            n.ngoEmail = TextBox27.Text.ToString();
            n.ngoCountry = TextBox28.Text.ToString();
            n.ngoCity = TextBox29.Text.ToString();
            n.ngoPhone = TextBox30.Text;
            //NGO.Insert(n);

            // CAPTCHA CODE

            if (this.CodeNumberTextBox.Text == this.Session["CaptchaImageText"].ToString())
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(TextBox27.Text);
                message.To.Add(new MailAddress("outreach360@gmail.com"));
                message.Subject = "New NGO has signed up!";
                message.Body = "Dear Outreach, a new NGO has signed up to our outreach." + "\n" + "\nThe name of the NGO is: " + TextBox21.Text + "\n" + "\n" + "Below are the details: \n Username: " + TextBox22.Text.ToString() + "\n Password: " + TextBox23.Text.ToString() + "\n Address: " + TextBox25.Text.ToString() + "\n Zipcode: " + TextBox26.Text.ToString() + "\n Email: " + TextBox27.Text.ToString() + " \n Country: " + TextBox28.Text.ToString() + "\n City: " + TextBox29.Text.ToString() + "\n Contact No: " + TextBox30.Text.ToString();

                // SMTP Configuration
                SmtpClient smtp = new SmtpClient();

                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");
                // MAIL MESSAGE CONFIGURATION

                // SEND EMAIL
                smtp.Send(message);
                
            }
            else
            {
                this.MessageLabel.CssClass = "error";
                this.MessageLabel.Text = "You have enter the wrong code. Please try again!";
                this.CodeNumberTextBox.Text = "";
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }

            Response.Redirect("RedirectDCNGO.aspx");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (this.CodeNumberTextBox.Text != null)
            {
                this.CodeNumberTextBox.Text = "";
            }
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {

        }

        
            
        
    }
}
